package com.itlong.cloud.property;

import com.itlong.cloud.POJO.PO.PropertyPaymentBillPO;
import com.itlong.cloud.iottp.IIotTpJPushClientServiceClient;
import com.itlong.cloud.project.property.IPropertyAccountServiceClient;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      定时推送缴费通知信息
 * </desc>
 *
 * @createDate 2019/06/13
 */
@EnableScheduling // 启用定时任务
@RestController
public class PropertyPushPayNoticeTimeTask {

    private static Logger LOG = LoggerFactory.getLogger(PropertyPushPayNoticeTimeTask.class);

    @Autowired
    private IPropertyAccountServiceClient iPropertyAccountServiceClient;

    @Autowired
    private IIotTpJPushClientServiceClient iIotTpJPushClientServiceClient;


    /**
     * <desc>
     *      定时推送缴费通知信息
     * </desc>
     *
     * @author wangzhi
     * @createDate 2019/06/13
     */
    @Scheduled(cron = "20 0/5 * * * ?") // 每5分钟执行一次，避开整分高峰，放在每5分20秒执行
    public void pushPayNoticeTimeTask() throws Exception{
        try {
//            LOG.info("【定时任务】定时推送缴费通知信息开始");
            List<PropertyPaymentBillPO> propertyPaymentBillPOList = iPropertyAccountServiceClient.getPushPayNoticeBill();
            if (propertyPaymentBillPOList != null && !propertyPaymentBillPOList.isEmpty()) {
                List<String> userIdProjectId = new ArrayList<>();
                Map<String,String> comands = new HashMap<>();
                comands.put("refreshSips","1");
                for (PropertyPaymentBillPO propertyPaymentBillPO : propertyPaymentBillPOList) {
                    if (userIdProjectId.contains(propertyPaymentBillPO.getUserId() + "," + propertyPaymentBillPO.getProjectId())) {
                        continue;
                    }
                    if (propertyPaymentBillPO.getPaidAmount() != null) {
                        userIdProjectId.add(propertyPaymentBillPO.getUserId() + "," + propertyPaymentBillPO.getProjectId());
                        iIotTpJPushClientServiceClient.pushWithTaskTO(propertyPaymentBillPO.getProjectId(), propertyPaymentBillPO.getPhoneNum(), comands,
                                "您有1笔物业费" + propertyPaymentBillPO.getPaidAmount() + "元待缴纳，点击查看详情和支付，感谢您对我们工作的支持，祝生活愉快！");
                    } else {
                        userIdProjectId.add(propertyPaymentBillPO.getUserId() + "," + propertyPaymentBillPO.getProjectId());
                        iIotTpJPushClientServiceClient.pushWithTaskTO(propertyPaymentBillPO.getProjectId(), propertyPaymentBillPO.getPhoneNum(), comands,
                                "您有新的通知消息，请及时查看！");
                    }
                }
            }
//            LOG.info("【定时任务】定时推送缴费通知信息成功");
        } catch (Exception e) {
            LOG.error("【定时任务】定时推送缴费通知信息失败");
            throw e;
        }
    }
}
